package bb.lanxing.activity.sport;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.activity.base.BaseActivity;
import bb.lanxing.adapter.sport.SportActivityAdapter;
import bb.lanxing.manager.RemoteServiceManager;
import bb.lanxing.manager.RemoteSharedPreference;
import bb.lanxing.manager.SharedManager;
import bb.lanxing.mvp.view.sport.ItemStyle;
import bb.lanxing.util.CalorieUtil;
import bb.lanxing.util.Log;
import bb.lanxing.utils.PanelItemTypeUtil;

public class IndoorTrainingActivity extends BaseActivity implements View.OnClickListener {
    private final static String TAG = IndoorTrainingActivity.class.getSimpleName();

    private TextView sportTypeText;
    private TextView mSportItemBig;
    private TextView mSportItemBigName;
    private RecyclerView mRecyclerViewBig;
    private SportActivityAdapter mSportActivityAdapter = new SportActivityAdapter();

    private ImageView sport_action_img;

    public Handler handler = new Handler(Looper.getMainLooper());

    private int sportType = 0;
    private List<Integer> mPanelItemTypeList;
    private int mItemType;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.logd(TAG, "onCreate");

        setContentView(R.layout.activity_indoor_training);

        sportTypeText = findViewById(R.id.sport_type);
        sportTypeText.setText(getIntent().getStringExtra("title"));

        mSportItemBig = findViewById(R.id.sport_item_big);
        mSportItemBig.setText(PanelItemTypeUtil.getItemValue(null, mItemType));
        mSportItemBigName = findViewById(R.id.sport_item_big_name);

        mRecyclerViewBig = findViewById(R.id.big_panel_list);
        mRecyclerViewBig.setLayoutManager(new GridLayoutManager(this, 3));
        mRecyclerViewBig.setAdapter(mSportActivityAdapter);
        updateItemType();

        sport_action_img = findViewById(R.id.sport_action_img);
        sport_action_img.setOnClickListener(this);

        RemoteServiceManager.getInstance().startSport(0, RemoteSharedPreference.getInstance().getSportTypes());
        CalorieUtil.updatePersonalBMR();//update BMR before starting
        setNightMode();
    }

    private void setNightMode() {
        if (SharedManager.getInstance().isNightMode()) {
            sportTypeText.setTextColor(getColor(R.color.white));
            mSportItemBig.setTextColor(getColor(R.color.white));
            mSportItemBigName.setTextColor(getColor(R.color.white));
            sport_action_img.setBackgroundResource(R.drawable.exo_ic_pause_circle_filled);
            sport_action_img.setImageBitmap(null);
        }
        ItemStyle itemStyle;
        if (SharedManager.getInstance().isBigFontMode()) {
            itemStyle = ItemStyle.ITEMS_BIG_FONT_STYLE[2][0];
        } else {
            itemStyle = ItemStyle.ITEMS_STYLE[2][0];
        }
        mSportItemBig.setTextSize(itemStyle.textSize);
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.logd(TAG, "onResume");

        updateItemType();
    }

    private void updateItemType() {
        mPanelItemTypeList = PanelItemTypeUtil.loadCache(App.getContext().getUserId(), sportType);
        mSportActivityAdapter.updateItem(mPanelItemTypeList);
        mItemType = mPanelItemTypeList.get(0);
        mSportItemBigName.setText(PanelItemTypeUtil.getItemName(mItemType));
    }

    @Override
    public void onDestroy() {
        Log.logd(TAG, "onDestroy");

        RemoteServiceManager.getInstance().stopSport();

        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.sport_action_img) {
            finish();
        }
    }
}